package com.jfcloud.mall.user.controller;

import com.jfcloud.common.annotation.CheckToken;
import com.jfcloud.common.aspect.TokenUser;
import com.jfcloud.common.http.RespData;
import com.jfcloud.mall.user.domain.JfcloudUser;
import com.jfcloud.mall.user.domain.dto.DeductForm;
import com.jfcloud.mall.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zhaojunfu
 * @Title: UserController
 * @ProjectName jfcloud
 * @Description: 用户账户相关API
 * @date 2020/9/1222:36
 */
@Api(tags = "用户账户相关API")
@Controller
@RequestMapping("/user")
public class UserController{
    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @ApiOperation("扣减余额")
    @PostMapping(path = "/deduct")
    @ResponseBody
    @CheckToken
    public RespData deduct(@RequestBody DeductForm form, HttpServletRequest request, HttpServletResponse response) {
        try {
            TokenUser tokenUser=TokenUser.load(request);
            JfcloudUser token = userService.deduct(tokenUser.getUserId(),form);
            return new RespData(token);
        }catch (Exception e) {
            LOGGER.error(e.getMessage(),e);
            return new RespData(e);
        }
    }

}
